package com.yonyougov.devops.cruxcisupport.roleapp;

import com.yonyougov.devops.cruxcisupport.role.RoleService;
import com.yonyougov.devops.cruxcisupport.roleapp.dto.AddMulRoleAppDto;
import com.yonyougov.devops.cruxcisupport.roleapp.vo.RoleAppPageByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.roleapp.vo.RoleAppVo;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * t_role_app服务实现
 */
@Service
public class RoleAppServiceImpl extends BaseServiceImpl<RoleAppEntity, BigInteger> implements RoleAppService {
    private final RoleAppDao roleAppDao;
    private final RoleService roleService;

    public RoleAppServiceImpl(RoleAppDao roleAppDao, RoleService roleService) {
        this.roleAppDao = roleAppDao;
        this.roleService = roleService;
    }

    @Override
    public BaseDao<RoleAppEntity, BigInteger> getDao() {
        return roleAppDao;
    }

    //<!--用户自定义代码开始-->

    /**
     * 查询角色和应用的关联关系
     * 条件: 角色id, 应用名称
     *
     * @param roleAppPageByRoleIdDto [com.yonyougov.devops.cruxcisupport.roleapp.vo.RoleAppPageByRoleIdDto]
     * @return [com.yonyougov.devops.fwc.core.entity.Pagination]
     */
    @Override
    public Pagination<RoleAppVo> findRoleAppPageByRoleId(RoleAppPageByRoleIdDto roleAppPageByRoleIdDto) {
        List<RoleAppVo> roleAppVoList = roleAppDao.findRoleAppPageByRoleId(roleAppPageByRoleIdDto);
        int totalCount = roleAppDao.countRoleAppByRoleId(roleAppPageByRoleIdDto);

        return new Pagination<RoleAppVo>(roleAppPageByRoleIdDto.getMax(), roleAppPageByRoleIdDto.getPageNum(), totalCount, roleAppVoList);
    }

    @Override
    public void addBatch(List<RoleAppEntity> params) {
        roleAppDao.addBatch(params);
    }

    /**
     * 根据主键删除关联关系(角色-应用)
     *
     * @param ids role_app表的主键集合
     */
    @Override
    public void deleteRoleAppByPrimaryKey(List<BigInteger> ids) {
        roleAppDao.deleteBatchRoleAppByIds(ids);
    }

    /**
     * 查询当前用户下的应用
     * 条件: 权限过滤
     *
     * @return java.util.List
     * @throws Exception
     */
    @Override
    public List<BigInteger> getCurrentUserAppIds() throws Exception {
        List<BigInteger> roleIds = roleService.getCurrentUserRoleIds();
        return roleAppDao.getCurrentUserAppIds(roleIds);
    }

    /**
     * 角色关联应用
     * 参数: 角色id, 应用id集合
     *
     * @param addMulRoleAppDto [com.yonyougov.devops.cruxcisupport.roleapp.dto.AddMulRoleAppDto]
     */
    @Override
    public void addMulRoleApp(AddMulRoleAppDto addMulRoleAppDto) {
        List<RoleAppEntity> roleAppEntities = new ArrayList<>();
        addMulRoleAppDto.getAppId().forEach(appId -> {
            RoleAppEntity roleAppEntity = new RoleAppEntity();
            roleAppEntity.setId(new CruxDevOpsIdGenerator().generatorId(roleAppDao));
            roleAppEntity.setRoleId(addMulRoleAppDto.getRoleId());
            roleAppEntity.setAppId(appId);
            roleAppEntities.add(roleAppEntity);
        });
        addBatch(roleAppEntities);
    }

    //<!--用户自定义代码结束-->
}
